<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        $this->display();
    }
    public function top(){
        $this->display();
    }
    public function left(){
        $this->display();
    }
    public function footer(){
        $this->display();
    }
    public function login(){
        $this->display();
    }
    public function user(){
        $userM=D('admin');
        $userRes=$userM->where('adminid != 1')->select();
        $this->assign('userData',$userRes);
        $this->display();
    }
    public function useradd(){
        if($_POST){
            $userM=D('admin');
            $whereAA['username']=I('post.username');
            $res=$userM->where($whereAA)->find();
            if($res){
                $this->error("账号已存在",U('Index/user'));
            }else{
               $data['username']=I('post.username');
                $data['password']=md5(I('post.password'));
                $userRes=$userM->add($data);
                if($userRes){
                    $this->success("保存成功",U('Index/user'));
                }else{
                    $this->error("保存失败",U('Index/user'));
                } 
            }
            exit();
        }
        $this->display();
    }
    public function userdel(){
        $userM=D('admin');
        $whereAA['adminid']=I('get.adminid');
        $result=$userM->where($whereAA)->delete();
        if($result){
            $this->success("删除成功",U('Index/user'));
        }else{
            $this->error("删除失败",U('Index/user'));
        }
    }
    public function password(){
        if($_POST){
            $userM=D('admin');
            $whereAA['adminid']=session("adminid");
            $userRes=$userM->where($whereAA)->find();
            if($userRes){
                if($userRes['password']==md5(I('post.oldpassword'))){
                    $data['password']=md5(I('post.password'));
                    $result=$userM->where($whereAA)->save($data);
                    if($result){
                        $this->success("保存成功",U('Index/user'));
                    }else{
                        $this->error("保存失败",U('Index/user'));
                    }
                }else{
                    $this->error("原密码错误",U('Index/password'));
                }
            }else{
                $this->error("原密码错误",U('Index/password'));
            }
            exit();
        }
        $this->display();
    }





}